﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Calendar;

namespace GM.Forms
{
    public partial class RepairForm : Form
    {

        /// <summary>
        /// Lista wszystkich napraw wyświetlanych na kontrolce kalendarza.
        /// </summary>
        List<Calendar.Appointment> m_Repairs;

        /// <summary>
        /// Domyślny konstruktor.
        /// </summary>
        public RepairForm()
        {
            InitializeComponent();

            m_Repairs = new List<Appointment>();

            dayView.StartDate = DateTime.Now;
            dayView.OnNewAppointment += new EventHandler<NewAppointmentEventArgs>(dayView_NewRepair);
            dayView.OnSelectionChanged += new EventHandler<EventArgs>(dayView_SelectionChanged);
            dayView.OnResolveAppointments += new EventHandler<ResolveAppointmentsEventArgs>(this.dayView_ResolveAppointments);

            dayView.MouseMove += new System.Windows.Forms.MouseEventHandler(this.dayView_MouseMove);

            // get process
            //m_processHandle = GetCurrentProcess();

        }

        /// <summary>
        /// Jeśli okno jest zamykane to zamknięcie zostaje anulowane a okno ukryte.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepairForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void RepairForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Appointment m_App = new Appointment();
            m_App.StartDate = dayView.SelectionStart;
            m_App.EndDate = dayView.SelectionEnd;
            m_App.BorderColor = Color.Red;

            m_App.Subject = "Nowa naprawa";
            m_Repairs.Add(m_App);

            dayView.Invalidate();
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            dayView.StartDate = monthCalendar1.SelectionStart;
        }

        private void dayView_ResolveAppointments(object sender, ResolveAppointmentsEventArgs args)
        {
            List<Appointment> m_Apps = new List<Appointment>();

            foreach (Appointment m_App in m_Repairs)
                if ((m_App.StartDate >= args.StartDate) &&
                    (m_App.StartDate <= args.EndDate))
                    m_Apps.Add(m_App);

            args.Appointments = m_Apps;
        }

        void dayView_NewRepair(object sender, NewAppointmentEventArgs args)
        {
            Appointment new_reapir = new Appointment();

            new_reapir.StartDate = args.StartDate;
            new_reapir.EndDate = args.EndDate;
            new_reapir.Subject = args.Title;
            new_reapir.Group = "2";

            m_Repairs.Add(new_reapir);
        }

        private void dayView_SelectionChanged(object sender, EventArgs e)
        {
            if (dayView.Selection == SelectionType.DateRange)
            {
                //label3.Text = dayView1.SelectionStart.ToString() + ":" + dayView1.SelectionEnd.ToString();
            }
            else if (dayView.Selection == SelectionType.Appointment)
            {
                //label3.Text = dayView1.SelectedAppointment.StartDate.ToString() + ":" + dayView1.SelectedAppointment.EndDate.ToString();
            }
        }

        private void dayView_MouseMove(object sender, MouseEventArgs e)
        {
            // label2.Text = dayView1.GetTimeAt(e.X, e.Y).ToString();
        }

    }
}
